﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace CiroKontrol
{
    public class Rapor : DevExpress.XtraReports.UI.XtraReport
    {
        private DevExpress.XtraReports.UI.DetailBand Detail;
        private DevExpress.XtraReports.UI.XRLabel lblTarih;
        private DevExpress.XtraReports.UI.XRLabel lblUnvani;
        private DevExpress.XtraReports.UI.XRLabel xrLabel2;
        private DevExpress.XtraReports.UI.XRLabel lblKodu;
        private DevExpress.XtraReports.UI.XRLabel xrLabel1;
        private DevExpress.XtraReports.UI.XRLabel xrLabel4;
        private DevExpress.XtraReports.UI.XRLabel xrLabel5;
        private DevExpress.XtraReports.UI.XRLabel xrLabel3;
        private DevExpress.XtraReports.UI.XRLine xrLine1;
        private DevExpress.XtraReports.UI.XRLabel lblCiro;
        private DevExpress.XtraReports.UI.XRLabel lblTedarikciTutar;
        private DevExpress.XtraReports.UI.XRLabel lblTedarikci;
        private DevExpress.XtraReports.UI.ReportFooterBand ReportFooter;
        private DevExpress.XtraReports.UI.XRPictureBox xrPictureBox1;
        private DevExpress.XtraReports.UI.XRLabel lblOzelMesaj;
        private DevExpress.XtraReports.UI.XRPictureBox xrPictureBox2;

        private DevExpress.XtraReports.UI.PageHeaderBand PageHeader;

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Rapor));
            this.PageHeader = new DevExpress.XtraReports.UI.PageHeaderBand();
            this.lblOzelMesaj = new DevExpress.XtraReports.UI.XRLabel();
            this.xrPictureBox1 = new DevExpress.XtraReports.UI.XRPictureBox();
            this.xrLabel5 = new DevExpress.XtraReports.UI.XRLabel();
            this.xrLabel3 = new DevExpress.XtraReports.UI.XRLabel();
            this.xrLine1 = new DevExpress.XtraReports.UI.XRLine();
            this.lblCiro = new DevExpress.XtraReports.UI.XRLabel();
            this.xrLabel4 = new DevExpress.XtraReports.UI.XRLabel();
            this.lblTarih = new DevExpress.XtraReports.UI.XRLabel();
            this.lblUnvani = new DevExpress.XtraReports.UI.XRLabel();
            this.xrLabel2 = new DevExpress.XtraReports.UI.XRLabel();
            this.lblKodu = new DevExpress.XtraReports.UI.XRLabel();
            this.xrLabel1 = new DevExpress.XtraReports.UI.XRLabel();
            this.Detail = new DevExpress.XtraReports.UI.DetailBand();
            this.lblTedarikciTutar = new DevExpress.XtraReports.UI.XRLabel();
            this.lblTedarikci = new DevExpress.XtraReports.UI.XRLabel();
            this.ReportFooter = new DevExpress.XtraReports.UI.ReportFooterBand();
            this.xrPictureBox2 = new DevExpress.XtraReports.UI.XRPictureBox();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            // 
            // PageHeader
            // 
            this.PageHeader.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrPictureBox1,
            this.lblOzelMesaj,
            this.xrLabel5,
            this.xrLabel3,
            this.xrLine1,
            this.lblCiro,
            this.xrLabel4,
            this.lblTarih,
            this.lblUnvani,
            this.xrLabel2,
            this.lblKodu,
            this.xrLabel1});
            this.PageHeader.Height = 210;
            this.PageHeader.Name = "PageHeader";
            // 
            // lblOzelMesaj
            // 
            this.lblOzelMesaj.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                        | DevExpress.XtraPrinting.BorderSide.Right)
                        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            this.lblOzelMesaj.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.lblOzelMesaj.Location = new System.Drawing.Point(5, 3);
            this.lblOzelMesaj.Name = "lblOzelMesaj";
            this.lblOzelMesaj.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblOzelMesaj.Size = new System.Drawing.Size(617, 46);
            this.lblOzelMesaj.StylePriority.UseBorders = false;
            this.lblOzelMesaj.StylePriority.UseFont = false;
            this.lblOzelMesaj.StylePriority.UseTextAlignment = false;
            this.lblOzelMesaj.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            // 
            // xrPictureBox1
            // 
            this.xrPictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("xrPictureBox1.Image")));
            this.xrPictureBox1.Location = new System.Drawing.Point(483, 51);
            this.xrPictureBox1.Name = "xrPictureBox1";
            this.xrPictureBox1.Size = new System.Drawing.Size(133, 55);
            this.xrPictureBox1.Sizing = DevExpress.XtraPrinting.ImageSizeMode.StretchImage;
            // 
            // xrLabel5
            // 
            this.xrLabel5.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.xrLabel5.Location = new System.Drawing.Point(475, 183);
            this.xrLabel5.Name = "xrLabel5";
            this.xrLabel5.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.xrLabel5.Size = new System.Drawing.Size(133, 18);
            this.xrLabel5.StylePriority.UseFont = false;
            this.xrLabel5.StylePriority.UseTextAlignment = false;
            this.xrLabel5.Text = "Kdv Hariç Net Ciro";
            this.xrLabel5.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
            // 
            // xrLabel3
            // 
            this.xrLabel3.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.xrLabel3.Location = new System.Drawing.Point(8, 183);
            this.xrLabel3.Name = "xrLabel3";
            this.xrLabel3.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.xrLabel3.Size = new System.Drawing.Size(133, 17);
            this.xrLabel3.StylePriority.UseFont = false;
            this.xrLabel3.Text = "Tedarikçi Firma";
            // 
            // xrLine1
            // 
            this.xrLine1.Location = new System.Drawing.Point(0, 200);
            this.xrLine1.Name = "xrLine1";
            this.xrLine1.Size = new System.Drawing.Size(617, 9);
            // 
            // lblCiro
            // 
            this.lblCiro.Location = new System.Drawing.Point(483, 150);
            this.lblCiro.Name = "lblCiro";
            this.lblCiro.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblCiro.Size = new System.Drawing.Size(134, 17);
            this.lblCiro.StylePriority.UseTextAlignment = false;
            this.lblCiro.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            // 
            // xrLabel4
            // 
            this.xrLabel4.Location = new System.Drawing.Point(483, 129);
            this.xrLabel4.Name = "xrLabel4";
            this.xrLabel4.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.xrLabel4.Size = new System.Drawing.Size(134, 17);
            this.xrLabel4.StylePriority.UseTextAlignment = false;
            this.xrLabel4.Text = "Toplam Ciro";
            this.xrLabel4.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            // 
            // lblTarih
            // 
            this.lblTarih.Location = new System.Drawing.Point(483, 107);
            this.lblTarih.Name = "lblTarih";
            this.lblTarih.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblTarih.Size = new System.Drawing.Size(134, 17);
            this.lblTarih.StylePriority.UseTextAlignment = false;
            this.lblTarih.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            // 
            // lblUnvani
            // 
            this.lblUnvani.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                        | DevExpress.XtraPrinting.BorderSide.Right)
                        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            this.lblUnvani.Location = new System.Drawing.Point(92, 78);
            this.lblUnvani.Multiline = true;
            this.lblUnvani.Name = "lblUnvani";
            this.lblUnvani.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblUnvani.Size = new System.Drawing.Size(275, 97);
            this.lblUnvani.StylePriority.UseBorders = false;
            this.lblUnvani.Text = "lblKodu\r\nsdsd\r\nsdsd\r\n";
            this.lblUnvani.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
            // 
            // xrLabel2
            // 
            this.xrLabel2.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            this.xrLabel2.Location = new System.Drawing.Point(9, 78);
            this.xrLabel2.Name = "xrLabel2";
            this.xrLabel2.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.xrLabel2.Size = new System.Drawing.Size(83, 17);
            this.xrLabel2.StylePriority.UseBorders = false;
            this.xrLabel2.Text = "Firma Kodu:";
            // 
            // lblKodu
            // 
            this.lblKodu.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                        | DevExpress.XtraPrinting.BorderSide.Right)
                        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            this.lblKodu.Location = new System.Drawing.Point(92, 58);
            this.lblKodu.Name = "lblKodu";
            this.lblKodu.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblKodu.Size = new System.Drawing.Size(275, 17);
            this.lblKodu.StylePriority.UseBorders = false;
            this.lblKodu.Text = "lblKodu";
            // 
            // xrLabel1
            // 
            this.xrLabel1.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                        | DevExpress.XtraPrinting.BorderSide.Bottom)));
            this.xrLabel1.Location = new System.Drawing.Point(9, 58);
            this.xrLabel1.Name = "xrLabel1";
            this.xrLabel1.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.xrLabel1.Size = new System.Drawing.Size(83, 17);
            this.xrLabel1.StylePriority.UseBorders = false;
            this.xrLabel1.Text = "Firma Kodu:";
            // 
            // Detail
            // 
            this.Detail.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.lblTedarikciTutar,
            this.lblTedarikci});
            this.Detail.Height = 17;
            this.Detail.Name = "Detail";
            // 
            // lblTedarikciTutar
            // 
            this.lblTedarikciTutar.Location = new System.Drawing.Point(475, 0);
            this.lblTedarikciTutar.Name = "lblTedarikciTutar";
            this.lblTedarikciTutar.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblTedarikciTutar.Size = new System.Drawing.Size(133, 17);
            this.lblTedarikciTutar.StylePriority.UseTextAlignment = false;
            this.lblTedarikciTutar.Text = "lblTedarikciTutar";
            this.lblTedarikciTutar.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
            // 
            // lblTedarikci
            // 
            this.lblTedarikci.Location = new System.Drawing.Point(8, 0);
            this.lblTedarikci.Name = "lblTedarikci";
            this.lblTedarikci.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
            this.lblTedarikci.Size = new System.Drawing.Size(442, 17);
            this.lblTedarikci.Text = "lblTedarikci";
            // 
            // ReportFooter
            // 
            this.ReportFooter.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrPictureBox2});
            this.ReportFooter.Height = 90;
            this.ReportFooter.Name = "ReportFooter";
            // 
            // xrPictureBox2
            // 
            this.xrPictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("xrPictureBox2.Image")));
            this.xrPictureBox2.Location = new System.Drawing.Point(483, 2);
            this.xrPictureBox2.Name = "xrPictureBox2";
            this.xrPictureBox2.Size = new System.Drawing.Size(142, 83);
            this.xrPictureBox2.Sizing = DevExpress.XtraPrinting.ImageSizeMode.StretchImage;
            // 
            // Rapor
            // 
            this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] {
            this.PageHeader,
            this.Detail,
            this.ReportFooter});
            this.PageHeight = 1169;
            this.PageWidth = 827;
            this.PaperKind = System.Drawing.Printing.PaperKind.A4;
            this.Version = "8.2";
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();

        }

        string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

        public Rapor()
        {
            InitializeComponent();
        }

        private void CariBilgileri(string cariKod)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT     CUSTOMERS.name AS [Cari Ünvanı], CAST(SUM(ORDER_DETAILS.total) AS decimal(16, 2)) AS Tutar, CUSTOMERS.adress, CUSTOMERS.telnumber, CUSTOMERS.taxoffice, CUSTOMERS.taxnumber, CUSTOMERS.faxnumber ");
            sb.Append(" FROM         ORDER_DETAILS INNER JOIN  CUSTOMERS ON ORDER_DETAILS.customer_code = CUSTOMERS.code ");
            sb.Append(" WHERE     (ORDER_DETAILS.customer_code = '{0}') ");
            sb.Append(" GROUP BY CUSTOMERS.name, CUSTOMERS.adress, CUSTOMERS.telnumber, CUSTOMERS.taxoffice, CUSTOMERS.taxnumber, CUSTOMERS.faxnumber ");
            sb.Append(" ORDER BY SUM(ORDER_DETAILS.total) DESC ");

            List<string> cariunvan = new List<string>();
            decimal ciro = 0;
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format(sb.ToString(), cariKod), cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            cariunvan.Add(rd.GetString(0));
                            cariunvan.Add(rd.GetString(2));
                            cariunvan.Add(rd.GetString(4));
                            cariunvan.Add(rd.GetString(5));
                            cariunvan.Add(rd.GetString(3));
                            cariunvan.Add(rd.GetString(6));
                            ciro = rd.GetDecimal(1);
                        }
                    }
                    rd.Close();
                }
                cn.Close();
            }
            lblTarih.Text = DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString("0000");
            lblKodu.Text = cariKod;
            lblUnvani.Lines = cariunvan.ToArray();
            lblCiro.Text = ciro.ToString("#,##0.00");
            if (File.Exists(Application.StartupPath + "\\siparismesaj.txt"))
            {
                lblOzelMesaj.Text = File.ReadAllText(Application.StartupPath + "\\siparismesaj.txt");
            }
            TedarikciListe(cariKod);
        }
        private void TedarikciListe(string cariKod)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT     CUSTOMERS.name, CAST(SUM(ORDER_DETAILS.total) AS decimal(16, 2)) AS ciro ");
            sb.Append(" FROM         ORDER_DETAILS INNER JOIN CUSTOMERS ON ORDER_DETAILS.supplier = CUSTOMERS.code ");
            sb.Append(" WHERE     (ORDER_DETAILS.customer_code = '{0}') GROUP BY CUSTOMERS.name  ORDER BY ciro DESC");
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format(sb.ToString(), cariKod), cn))
                {
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                    }
                }
                cn.Close();
            }

            this.DataSource = dt;
            lblTedarikci.DataBindings.Add("Text", dt, "name");
            lblTedarikciTutar.DataBindings.Add("Text", dt, "ciro", "{0:n2}");

        }


        public void LoadData(string cariKod)
        {
            CariBilgileri(cariKod);
        }
    }
}
